/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.editor;

import easybooking.data.Airline;
import easybooking.data.Airport;
import easybooking.data.Flight;
import easybooking.data.Plane;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 * Table Model for flight objects
 * @author Tim Schröder
 */
public class FlightTableModel extends AbstractTableModel {

    /**
     * Vector which contains all flights
     */
    private Vector<Flight> flights;
    private Plane p;
    private Airline aril;
    private Airport sa;
    private Airport da;
    private EBInterfaceClient ebic;
    /**
     * The index of the column where the ID get insert
     */
    private static final int ID_INDEX = 0;
    /**
     * The index of the column where the Flight Number get insert
     */
    private static final int FLIGHT_NO_INDEX = 1;
    /**
     * The index of the column where the Date get insert
     */
    private static final int DATE_INDEX = 2;
    /**
     * The index of the column where the airline get insert
     */
    private static final int AIRLINE_INDEX = 3;
    /**
     * The index of the column where the Plane get insert
     */
    private static final int PLANE_INDEX = 4;
    /**
     * The index of the column where the Startairport get insert
     */
    private static final int START_INDEX = 5;
    /**
     * The index of the column where the Destinationairport get insert
     */
    private static final int STOP_INDEX = 6;
    /**
     * Index to get the Object which is selected
     */
    private static final int OBJECT = 7;
    /**
     * Names of columns
     */
    private String[] columnNames = {"ID", "Flight No.", "Date", "Airline", "Plane", "Start", "Stop"};

    public FlightTableModel(Vector<Flight> f, EBInterfaceClient ebifc) {
        this.ebic = ebifc;
        flights = f;
    }

    /**
     * get the number of rows the table needs
     * @return the number of flight objects in the flights Vector
     */
    public int getRowCount() {
        return flights.size();
    }

    /**
     * get the number of columns
     * @return the number of the column Names
     */
    public int getColumnCount() {
        return columnNames.length;

    }

    @Override
    /**
     * get the name of a column
     * @param col is the number of the column which name is wanted
     * @return the name of a column
     */
    public String getColumnName(int col) {
        return columnNames[col];
    }

    /**
     * prepares the airline data for the table
     * @param rowIndex the row in which the data get insert
     * @param columnIndex the column in which the data get insert
     * @return the prepared object
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            Flight f = (Flight) flights.get(rowIndex);
            switch (columnIndex) {

                case ID_INDEX:
                    return f.getFlightId();
                case FLIGHT_NO_INDEX:
                    p = ebic.findPlaneByKey(f.getPlaneId());
                    aril = ebic.findAirlineByKey(p.getAirlineId());
                    return aril.getInitials() + f.getFlightId();
                case DATE_INDEX:
                    return f.getDateTime();
                case AIRLINE_INDEX:
                    p = ebic.findPlaneByKey(f.getPlaneId());
                    aril = ebic.findAirlineByKey(p.getAirlineId());
                    return aril.getName();
                case PLANE_INDEX:
                    p = ebic.findPlaneByKey(f.getPlaneId());
                    return p.getName();
                case START_INDEX:
                    sa = ebic.findAirportByKey(f.getStartAirportId());
                    return sa.getName();
                case STOP_INDEX:
                    da = ebic.findAirportByKey(f.getDestAirportId());
                    return da.getName();
                case OBJECT:
                    return f;
                default:
                    return new Object();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(FlightTableModel.class.getName()).log(Level.SEVERE, null, ex);
            return new Object();
        }
    }
}

